import axios from 'axios'
import JSBridge from './JSBridge'
import './Authorization'
import {
  pushToGoodsDetailPage,
  pushToDefaultPage,
  pushToCategorySecondPage,
  pushToShopCartPage
} from './naviPush'
import {
  Toast
} from 'vant'

let YSModule = {}
const jsBridge = JSBridge.getInstance()
require('es6-promise').polyfill()

YSModule.install = function (Vue, options) {
  Vue.config.productionTip = false

  Vue.prototype.axios = axios
  Vue.prototype.jsBridge = jsBridge

  Vue.prototype.pushWeblink = function (link) {
    if (process.env.NODE_ENV === 'development') {
      if (typeof link === 'string' && link.constructor === String) {
        window.location.href = link
      } else if (typeof link === 'object' && link.constructor === Object) {
        if (link.type === 'goods') {
          this.$router.push({
            name: 'goods/:id',
            params: {
              id: link.id
            }
          })
        } else if (link.type === 'category') {
          this.$router.push({
            name: 'shop/category/:id',
            params: {
              id: link.id
            }
          })
        } else if (link.type === 'cart') {
          this.$router.push({
            name: 'cart'
          })
        }
      } else {
        Toast('跳转类型错误')
      }
    } else {
      if (typeof link === 'string' && link.constructor === String) {
        pushToDefaultPage(link)
      } else if (typeof link === 'object' && link.constructor === Object) {
        if (link.type === 'goods') {
          pushToGoodsDetailPage(link.id)
        } else if (link.type === 'category') {
          pushToCategorySecondPage(link.id)
        } else if (link.type === 'cart') {
          pushToShopCartPage()
        }
      } else {
        Toast('跳转类型错误')
      }
    }
  }

  // 给iOS监听请求完成
  jsBridge.finishLoaded(null)
}

export default YSModule
